/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */

 var isSymmetric = function(root) {  //迭代
    if(!root){
        return true;
    }
    let arr=[root,root];
    let head=0,end=1,second=1;
    while(head!=end){
        let left=arr[head],right=arr[second];
        
        if(left && right){
            if(left.val!=right.val){
                return false;
            } 
            arr.push(left.left);
            arr.push(right.right);
            arr.push(left.right);
            arr.push(right.left);
        }
        else if(!(!left && !right)){
            return false;
        }
        head +=2;
        second +=2;
        end=arr.length;

    }
    return true;
}
 var isSymmetric = function(root) {  //递归
    if(!root){
        return true;
    }
    let res=true;
    function digui(left,right){
       if(!left && !right){  //端点
           return ;
       }
       if(!res){
           return ;
       }
       else if(left && right){  // 递归
           if(left.val!=right.val){
               res=false;
               return ;
           }
           digui(left.left,right.right);
           digui(left.right,right.left);
       } 
       else{                      // 左右不等
           res=false;
           return ;
       }
    }
    digui(root.left,root.right);
    return res;
};